//
//  WWCollectedCitiesManage.m
//  WhatWeather
//
//  Created by byn on 16/1/3.
//  Copyright © 2016年 byn. All rights reserved.
//

#import "WWCollectedCitiesManage.h"
#import "FileUtil.h"

@interface WWCollectedCitiesManage()

@property (nonatomic,strong) NSString *fileWithPath;
@property (nonatomic,strong) NSMutableDictionary *collectedCitiesDic;
@property (nonatomic,strong) NSString *curSelCity;
@property (nonatomic,strong) NSString *curSelCityWeather;

@end
NSString *propertyListName = @"collectedCities.plist";
NSString *rootKey = @"allCities";
NSString *citiesKey = @"citiesKey";
NSString *curSelCity = @"curSelCity";
@implementation WWCollectedCitiesManage

- (NSMutableDictionary *)collectedCitiesDic{
    if (!_collectedCitiesDic) {
        _collectedCitiesDic = [[NSMutableDictionary alloc] init];
        _collectedCitiesDic = [self readCollectedCites];
    }
    return _collectedCitiesDic;
}
- (void)setCollectedCitisDic:(NSMutableDictionary *)collectedCitisDic {
    self.collectedCitisDic = collectedCitisDic;
}
- (NSMutableArray *)allCollectedCity {
    if (0 == self.collectedCitiesDic.count) {
        return [[self readCollectedCites] objectForKey:citiesKey];
    }
    return [self.collectedCitiesDic objectForKey:citiesKey];
}

- (NSString *)fileWithPath{
    if (!_fileWithPath) {
        _fileWithPath = [FileUtil createFileInPath:propertyListName];
    }
    return _fileWithPath;
}
+ (instancetype)sharedCollectedCities {
    static WWCollectedCitiesManage *_sharedCollectedCities = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedCollectedCities = [[WWCollectedCitiesManage alloc] init];
    });
    
    return _sharedCollectedCities;
}

- (void)addNewCity: (NSString *)cityID cityName: (NSString *)cityName {
    
    NSMutableArray *cityArray = [self.collectedCitiesDic objectForKey:citiesKey];
    
    if (nil == cityArray) {
        cityArray = [[NSMutableArray alloc] init];
        [self.collectedCitiesDic setObject:cityArray forKey:citiesKey];
    }
    for (NSMutableDictionary *dic in cityArray) {
        if ([[dic objectForKey:@"cityId"] isEqualToString:cityID]) {
            return;
        }
    }
    NSMutableDictionary *cityDic = [[NSMutableDictionary alloc] init];
    [cityDic setObject:cityName forKey:@"cityName"];
    [cityDic setObject:cityID forKey:@"cityId"];
    [cityArray addObject:cityDic];
    
    [self saveCollectedCityID];
}

- (void)addCurSelCity : (NSString *)cityId {
    [self.collectedCitiesDic setObject:cityId forKey:curSelCity];
    [self saveCollectedCityID];
}

- (void)removeCity: (NSInteger)index {
    
    NSMutableArray *cityArray = [self.collectedCitiesDic objectForKey:citiesKey];
    [cityArray removeObjectAtIndex:index];
    [self saveCollectedCityID];
}

- (void)saveCollectedCityID {
    NSError *error;
    NSData *data = [NSPropertyListSerialization dataWithPropertyList:_collectedCitiesDic format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
    if (data) {
        [data writeToFile:_fileWithPath atomically:YES];
    }
}
- (NSMutableDictionary *)readCollectedCites{
    if (self.fileWithPath) {
        NSData *dataXML = [[NSFileManager defaultManager] contentsAtPath:self.fileWithPath];
        NSError *error;
        NSPropertyListFormat plistFormat;
        NSDictionary *dicTemp = (NSDictionary *)[NSPropertyListSerialization propertyListWithData:dataXML options:NSPropertyListMutableContainersAndLeaves format:&plistFormat error:&error];
        if (nil != dicTemp) {
            _collectedCitiesDic = dicTemp.mutableCopy;
        }
    }
    return _collectedCitiesDic;
}
- (void)setCityWeather : (NSString *)weather withCityId : (NSString *)cityId {
    
    for (NSMutableDictionary *cityDic in [self.collectedCitiesDic objectForKey:citiesKey]) {
        
        if ([[cityDic objectForKey:@"cityId"] isEqualToString:cityId]) {
            [cityDic setObject:weather forKey:@"cityWeather"];
            break;
        }
    }
}

- (NSString *)getCurSelCityId {
    return [self.collectedCitiesDic objectForKey:curSelCity];
}
@end











































